package com.eiei.manager;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.eiei.bean.MessageBean;
import com.eiei.common.CommonValue;
import com.eiei.db.dao.MessageBeanDao;
import com.eiei.db.dao.MessageBeanDaoImpl;
import com.eiei.db.pool.ConnectionPool;
import com.eiei.utils.EmailUtils;
import com.eiei.utils.TimeSequenceUtils;

/**
 * 信息的业务逻辑处理类
 * @author eiei
 *
 */
public class MessageManager {
	
	private static MessageBeanDao dao = new MessageBeanDaoImpl();
	
	private static final Logger logger = Logger.getLogger(MessageManager.class);

	/**
	 * 发送信息
	 * @param message
	 * @throws Exception
	 */
	public void sendMessage(MessageBean message) throws Exception {
	
		if(message==null){
			return ;
		}
		if(message.getId()==null || message.getId().length()==0){
			message.setId(TimeSequenceUtils.getIdString());
		}
		
		if(message.getSender() == null || message.getSender().length()==0){
			message.setSender(CommonValue.PROPERTIES_MAP.get("current_account"));
		}
		
		if(message.getReciver() == null || message.getReciver().length()==0){
			message.setReciver(CommonValue.PROPERTIES_MAP.get("to_account"));
		}
		message.setStatus(MessageBean.READED);
		
		Connection con = ConnectionPool.getConnection();
		try{
			EmailUtils.sendMessage(message);
			message.setId("T"+message.getId());
			dao.save(message, con);
			con.commit();
		}catch(Exception e){
			con.rollback();
		}finally{
			ConnectionPool.freeConection(con);
		}
		
	}
	
	public void saveMessages(List<MessageBean> messages) throws Exception{
		if(messages!=null && messages.size()!=0){
			Connection con = ConnectionPool.getConnection();
			try{
				for(MessageBean message:messages){
					dao.save(message, con);
				}
			}catch(Exception e){
			}finally{
				con.commit();
				ConnectionPool.freeConection(con);
			}
		}
	}
	
	public int reviceMessages(){
		try{
			List<MessageBean> list = EmailUtils.recive();
			if(list!=null && list.size()!=0){
				Connection con = ConnectionPool.getConnection();
				try{
					for(MessageBean message:list){
						dao.save(message, con);
					}
				}catch(Exception e){
					throw e;
				}finally{
					con.commit();
					ConnectionPool.freeConection(con);
				}
			}
			if(list!=null){
				return list.size();
			}else{
				return 0;
			}
		}catch(Exception e){
			logger.error("接受邮件抛出异常。"+e);
		}
		return 0;
	}
	
	/**
	 * 根据创建时间排序，并且设置为已读
	 * @param index
	 * @param size
	 * @return
	 * @throws Exception 
	 */
	public List<MessageBean> list(Integer index,Integer size) throws Exception{
		
		Connection con = null;
		List<MessageBean> result = null;
		try{
			con = ConnectionPool.getConnection();
			List<MessageBean> list = dao.list(index, size, con);
			List<String> updateIds = new ArrayList<String>();
			if(list!=null && list.size()!=0){
				result = new ArrayList<MessageBean>(list.size());
				for(int i = list.size()-1 ; i>=0 ;i--){
					result.add(list.get(i));
					if(MessageBean.UNREADER.equals(list.get(i).getStatus())){
						updateIds.add(list.get(i).getId());
					}
				}
			}
			dao.updateStat(updateIds, con);
			con.commit();
		}catch(Exception e){
			if(con!=null){
				con.rollback();
			}
			throw e;
		}finally{
			ConnectionPool.freeConection(con);
		}
		return result;
		
		
	}
	
	
	public int count() throws Exception{
		Connection con = null;
		try{
			con = ConnectionPool.getConnection();
			int count = dao.countUnRead(con);
			con.commit();
			return count;
		}catch(Exception e){
			throw e;
		}finally{
			ConnectionPool.freeConection(con);
		}
	}
	
}
